package com.nq.fund.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.common.Result;
import com.nq.common.ServerResponse;
import com.nq.fund.entity.StFund;
import com.nq.fund.entity.StFundServiceCharge;
import com.nq.fund.entity.StFundServiceChargeDetails;
import com.nq.fund.service.IStFundService;
import com.nq.service.StFundApplyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

 /**
 * @Description: st_fund
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@RestController
@RequestMapping("/admin/stFund")
@Slf4j
public class StFundController {
	@Autowired
	private IStFundService stFundService;

	@Autowired
	private StFundApplyService stFundApplyService;


	/**
	 * 分页列表查询
	 *
	 * @param stFund
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@PostMapping(value = "/list")
	public Result<?> queryPageList(StFund stFund,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		String fundName = stFund.getFundName();
		String fundCode = stFund.getFundCode();
		QueryWrapper<StFund> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(fundName),"fund_name", fundName);
		queryWrapper.eq(StringUtils.isNotBlank(fundCode),"fund_code", fundCode);
		Page<StFund> page = new Page<StFund>(pageNo, pageSize);
		IPage<StFund> pageList = stFundService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stFund
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(StFund stFund) {
		stFundService.addFund(stFund);
		return Result.OK("Added successfully！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stFund
	 * @return
	 */
	@PostMapping(value = "/edit")
	public Result<?> edit(StFund stFund) {
		stFundService.updateById(stFund);
		return Result.OK("Edited successfully!");
	}

	/**
	 *  编辑简介
	 *
	 * @param stFund
	 * @return
	 */
	@PostMapping(value = "/editFundIntroduction")
	public Result<?> editFundIntroduction(StFund stFund) {
		StFund updateFund = new StFund();
		updateFund.setId(stFund.getId());
		updateFund.setIntroduction(stFund.getIntroduction());
		stFundService.updateById(updateFund);
		return Result.OK("Edited successfully!");
	}

	/**
	 *  查询基金服务费
	 * @param charge
	 * @return
	 */
	@PostMapping(value = "/queryFundServiceCharge")
	public ServerResponse queryFundServiceCharge(StFundServiceCharge charge, HttpServletRequest request) {
		Integer fundId = charge.getFundId();
		if (null == fundId) {
			return ServerResponse.createByErrorMsg("Parameter error【fundId】");
		}

		ServerResponse serverResponse = stFundApplyService.queryFundServiceCharge(fundId, request);
		return serverResponse;
	}
	/**
	 *  编辑服务费
	 * @param serviceCharge
	 * @return
	 */
	@PostMapping(value = "/editFundServiceCharge")
	public Result<?> editFundServiceCharge(StFundServiceCharge serviceCharge) {
		stFundService.editFundServiceCharge(serviceCharge);
		return Result.OK("Edited successfully!");
	}

	/**
	 *  查询基金服务费一对多详情
	 * @param charge
	 * @return
	 */
	@PostMapping(value = "/queryFundServiceChargeDetails")
	public ServerResponse queryFundServiceChargeDetails(StFundServiceChargeDetails charge, HttpServletRequest request) {
		Integer chargeId = charge.getChargeId();
		if (null == chargeId) {
			return ServerResponse.createByErrorMsg("Parameter error【chargeId】");
		}

		ServerResponse serverResponse = stFundApplyService.queryFundServiceChargeDetails(chargeId, request);
		return serverResponse;
	}
	/**
	 *  编辑服务费一对多详情
	 * @param serviceCharge
	 * @return
	 */
	@PostMapping(value = "/editFundServiceChargeDetails")
	public Result<?> editFundServiceChargeDetails(StFundServiceChargeDetails serviceCharge) {
		stFundService.editFundServiceChargeDetails(serviceCharge);
		return Result.OK("Edited successfully!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		stFundService.removeById(id);
		return Result.OK("Deleted successfully!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stFundService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("Batch Delete Successfully!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StFund stFund = stFundService.getById(id);
		if(stFund==null) {
			return Result.error("No corresponding data found");
		}
		return Result.OK(stFund);
	}

}
